package com.ptone.messagecenter.modules.msg.util;

import com.alibaba.fastjson.JSONObject;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MsgUtil {
    public static String initMsg(String msgTemplate, JSONObject msgTemplateValue) {
        Pattern pattern = Pattern.compile("\\$\\{(.*?)}");
        Matcher matcher = pattern.matcher(msgTemplate);
        while (matcher.find()) {
            //占位符
            String placeholder = matcher.group();
            String key = placeholder.substring(2, placeholder.length() - 1);
            if (msgTemplateValue.containsKey(key)) {
                String value = msgTemplateValue.getString(key).trim();
                //将占位符替换为JsonObject里的值
                msgTemplate = msgTemplate.replace(placeholder, value);
            }
        }
        return msgTemplate;
    }
}
